#include <iostream>
#include <stdint.h>
#include <bitset>
#include "common.h"
using namespace std;


// #define COMP_CODE

// 转十进制为二进制

void binary_to(int num)
{
    cout << "二进制: ";
    
    for (int i = 31; i >= 0; i--) {
        if ((num & (1 << i)) != 0) {
            cout << "1";
        } else {
            cout << "0";
        }
    }
    cout << endl;
}


int main (int argc, char *argv[])
{
    #if 0
    {
        int a = 1; // 0x00000001

        // char一定取地地址
        char b = *(char*)(&a);

        // big endian 还是 small endian 取决于系统结构，对于x86一般是小端字节序，有些网络编程是大端字节序
        if (b == 1)
        {
            cout << "小端"  << endl;

        }
        else
        {
            cout << "大端" << endl;
        }

        int i = 0x80000000; // 0b1000 0000 0000 0000 0000 0000 0000 0000
        i = i >> 1;//i的值不会变成0x40000000,而会变成0xc0000000
        cout << hex << i << endl;

        uint32_t ii = 0x80000000;

        // {
        //     bitset<32> binary(ii);
        //     cout << "二进制: " << binary << endl;

        //     binary_to(ii);
        // }
        

        ii = ii >> 1;

        cout << hex << ii << endl;
    }
    #endif
    
    {
        __LOG__("算术移位和逻辑移位的");
        // 负数的补码表示法：    除符号位其他位反码+1
        // -3的二进制原码: 10000000000000000000000000000011
        // -3的二进制反码: 11111111111111111111111111111100
        // -3的二进制补码: 11111111111111111111111111111101
        int a = -3; // 二进制表示为 11111111111111111111111111111011
        int b = 3;
        cout << "   描述  " << "              二进制              " <<    "     十六进制     " << " 十进制 " << endl;
        cout << "+3的补码 : " << bitset<32>(b) << "      " << hex << b << "              " << dec << b << endl;
        cout << "-3的补码 : " << bitset<32>(a) << "      " << hex << a << "      " << dec << a <<  endl;

        a = a << 2; // 左移2位，结果为 -6，二进制表示为 11111111111111111111111111110000
        cout << "-3左移2位: " << bitset<32>(a) << "      " << hex << a <<  "      " << dec << a <<  endl;

        b = b << 2;
        cout << "+3左移2位: " << bitset<32>(b) << "      " << hex << b <<  "             " << dec << b <<  endl;

        a = -3;
        a = a >> 2; // 右移2位，结果为 -3，二进制表示为 11111111111111111111111111100000
        cout << "-3右移2位: " << bitset<32>(a) << "      " << hex << a <<  "      " << dec << a <<  endl;

        b = 3;
        b = b >> 2;
        cout << "+3右移2位: " << bitset<32>(b) << "      " << hex << b <<  "              " << dec << b <<  endl;
    }
    

#ifdef COMP_CODE

    {
        int i = 0x80000000; // 0b1000 0000 0000 0000 0000 0000 0000 0000
        cout << hex << i << endl;
        i = i >> 1;//i的值不会变成0x40000000,而会变成0xc0000000
        cout << i << endl; 

        uint32_t ii = 0x80000000;
        cout << ii << endl;
        ii = ii >> 1;
        cout << ii << endl;
    }

    {
        __LOG__("有符号和无符号数的二进制补码表示");
        int8_t ko1 = -128;
        int8_t ko2 = -127;
        int8_t ko3 = -126;
        int8_t ko4 = -1;
        int8_t ko5 = +1;

        uint8_t ko6 = 128;
        uint8_t ko7 = 127;
        uint8_t ko8 = 126;
        uint8_t ko9 = 1;
        cout << "有符号-128的补码: " << bitset<8>(ko1) << endl;
        cout << "有符号-127的补码: " << bitset<8>(ko2) << endl;
        cout << "有符号-126的补码: " << bitset<8>(ko3) << endl;
        cout << "有符号  -1的补码: " << bitset<8>(ko4) << endl;
        cout << "有符号  +1的补码: " << bitset<8>(ko5) << endl;

        cout << "无符号 128的补码: " << bitset<8>(ko6) << endl;
        cout << "无符号 127的补码: " << bitset<8>(ko7) << endl;
        cout << "无符号 126的补码: " << bitset<8>(ko8) << endl;
        cout << "无符号   1的补码: " << bitset<8>(ko9) << endl;

        __LOG__("无符号和有符号加法溢出后舍掉溢出位");
        
        __LOG__("s8 的溢出 -128~127");// 补码将减法转换成加法，舍去溢出位 
        int8_t lp = -128-1; // 10000000 + 11111111 = 101111111 舍掉溢出的位为 01111111 故为127
        printf("%d\n",lp);  // 127
        int8_t lp1 = 127+1; // 00000001 + 01111111 = 100000000 为 -128
        printf("%d\n",lp1); // -128

        __LOG__("-128的诞生"); // -127 -1 = -128    10000001 + 11111111 = 110000000 舍掉溢出的位为 10000000 故为-128
        int8_t lp2 = -127;
        int8_t lp3 = -1;

        cout << "-127的二进制: " << bitset<8>(lp2) << endl;
        cout << "-1的二进制: " << bitset<8>(lp3) << endl;
        cout << "-128的十进制: " << dec << (lp2+lp3) << endl;
        cout << "-128的十六进制: " << hex << (lp2+lp3) << endl;
        cout << "-128的二进制: " << bitset<8>(lp2+lp3) << endl;

        __LOG__("u8 的溢出 0~255");
        uint8_t kp = 0-1;    // 00000000 + 11111111 = 11111111 故为255
        printf("%d\n",kp);   // 255
        uint8_t kp1 = 255+1; // 11111111 + 00000001 = 100000000 舍掉溢出的位为 00000000 故为0
        printf("%d\n",kp1);  // 0

        
       
    }
#endif


    {

        
    }

    return 0;
}